package fr.upmc.aladyn.transactionables.tests;

import java.awt.Color;
import java.util.Date;


import fr.upmc.aladyn.transactionables.annotations.Transactionable;
import fr.upmc.aladyn.transactionables.tests.objects.ColoredPoint;
import fr.upmc.aladyn.transactionables.tests.objects.Point;

/**
 * <p>Test class</p>
 * @author Alexis
 *
 */
@Transactionable
public class Test {
	
	public static final String constante = "00000000";
	protected int i;
	String s;
	public Date d;
	private Point p;
	private ColoredPoint c;
	
	public void setI(int ii){ i = ii;}
	public void setS(String ss){ s = ss;}
	public void setD(Date dd){ d = dd;}
	//public void setP(Point pp, int x, int y){ pp.setX(x); pp.setY(y); }
	//public void setC(ColouredPoint cp, int x, int y, Color c){ cp.setX(x); cp.setY(y); cp.setColor(c);}
	
	public Test(){
		i = 0;
		s = "initial";
		d = new Date();
		p = new Point(0,0);
		c = new ColoredPoint(0, 0, new Color(0));
		System.out.println("[TEST] Etat initial de l'objet");
		System.out.println("\ti = "+this.i);
		System.out.println("\ts = "+this.s);
		System.out.println("\td = "+this.d);
		System.out.println("\tp = "+this.p);
		System.out.println("\tc = "+this.c);
	}
	
	public void m2() {
		// instruction ne levant pas d'exception
		setI(9999999);
		
		// exception
		System.out.println("---------------------/!\\ Exception /!\\---------------------");
		int [] tab = new int[2];
		tab[0] = 1;
		tab[1] = 2;
		tab[3] = 3; // <- exception ici
	}
	
	@Transactionable
	public void m1_transactionnee(){
		// modifications des valeurs par la methode
		System.out.println("[TEST] Appel de setters pour modifier les champs de la classe Test");
		
		setI(10);
		setS("MODIFIED");
		setD(new Date(0));
		//setP(p, -1, -1);
		p.setX(-1);
		p.setY(-1);
		//setC(c, -2, -2, new Color(255));
		c.setX(-2);
		c.setY(-2);
		c.setColor(new Color(255));
		System.out.println("[TEST] Modification de l'objet par la methode m1_transactionnee");
		System.out.println("\ti = "+this.i);
		System.out.println("\ts = "+this.s);
		System.out.println("\td = "+this.d);
		System.out.println("\tp = "+this.p);
		System.out.println("\tc = "+this.c);
		try {
			this.m2();
		} catch (IllegalArgumentException e) {
			System.out.println("[TEST] Exception catchee dans m1_transactionnee de type : "+e.getClass().getName());
			throw e;
		} catch (Exception e) {
			System.out.println("[TEST] Exception catchee dans m1_transactionnee de type : "+e.getClass().getName());
			throw e;
		} catch (Throwable e) {
			System.out.println("[TEST] Exception catchee dans m1_transactionnee de type : "+e.getClass().getName());
			throw e;
		}

		// verification des valeurs
		System.out.println("[TEST] Verification des valeurs apres restauration");
		System.out.println("\ti = "+this.i);
		System.out.println("\ts = "+this.s);
		System.out.println("\td = "+this.d);
		System.out.println("\tp = "+this.p);
		System.out.println("\tc = "+this.c);
	}
	
	public void m1_non_transactionnee(){
		// modifications des valeurs par la methode
		System.out.println("[TEST] Appel de setters pour modifier les champs de la classe Test");
		setI(10);
		setS("MODIFIED");
		setD(new Date(0));
		//setP(p, -1, -1);
		p.setX(-1);
		p.setY(-1);
		//setC(c, -2, -2, new Color(255));
		c.setX(-2);
		c.setY(-2);
		c.setColor(new Color(255));
		System.out.println("[TEST] Modification de l'objet par la methode m1_non_transactionnee\n\ti = "+this.i);
		System.out.println("\ts = "+this.s);
		System.out.println("\td = "+this.d);
		System.out.println("\tp = "+this.p);
		System.out.println("\tc = "+this.c);
		try {
			this.m2();
		} catch (IllegalArgumentException e) {
			System.out.println("[TEST] Exception catchee dans m1_transactionnee de type : "+e.getClass().getName());
		} catch (Exception e) {
			System.out.println("[TEST] Exception catchee dans m1_transactionnee de type : "+e.getClass().getName());
		} catch (Throwable e) {
			System.out.println("[TEST] Exception catchee dans m1_transactionnee de type : "+e.getClass().getName());
		}

		// verification des valeurs
		System.out.println("[TEST] Verification des valeurs apres restauration");
		System.out.println("\ti = "+this.i);
		System.out.println("\ts = "+this.s);
		System.out.println("\td = "+this.d);
		System.out.println("\tp = "+this.p);
		System.out.println("\tc = "+this.c);
	}
	
	
	public static void main(String[]args){
		System.out.println("###########################################################");
		System.out.println("# Version non transactionnee :                            #\n");
		Test t1 = new Test();
		try {
			t1.m1_non_transactionnee();
		}catch(Exception e){
			
		}
		// verification des valeurs
		System.out.println("[TEST] Verification des valeurs apres restauration");
		System.out.println("\ti = "+t1.i);
		System.out.println("\ts = "+t1.s);
		System.out.println("\td = "+t1.d);
		System.out.println("\tp = "+t1.p);
		System.out.println("\tc = "+t1.c);
		System.out.println("#                                                         #");
		System.out.println("###########################################################");
		System.out.println("# Version transactionnee :                                #\n");
		Test t2 = new Test();
		try{
			t2.m1_transactionnee();
		}catch(Exception e){
			
		}
		// verification des valeurs
		System.out.println("[TEST] Verification des valeurs apres restauration");
		System.out.println("\ti = "+t2.i);
		System.out.println("\ts = "+t2.s);
		System.out.println("\td = "+t2.d);
		System.out.println("\tp = "+t2.p);
		System.out.println("\tc = "+t2.c);
		System.out.println("#                                                         #");
		System.out.print("###########################################################");
	}
}